package com.shop2cn.iapi.shenqisettlement.verify;

import java.math.BigDecimal;
import java.util.HashMap;

import com.shop2cn.iapi.shenqisettlement.parameter.OnHpSettledReq;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.ymt.core.tool.Logger;
import com.ymttest.database.model.shenqisettlement.HpSettlementNotify;
import com.ymttest.database.model.shenqisettlement.OrderSettlement;
import com.ymttest.database.model.shenqisettlement.OrderSettlementDetail;
import com.ymttest.database.model.shenqisettlement.PlatformProductDetail;
import com.ymttest.database.sqlwapper.AccountWapper;
import com.ymttest.database.sqlwapper.shenqisettlement.HpSettlementNotifyWrapper;
import com.ymttest.utils.DataUtil;

public class ShenqiSettlementVerifyTableService {

	public static void verifyOrderSettlementTable(
			OrderSettlement expectOrderSettlement,
			OrderSettlement actualOrderSettlement, boolean settled) {

		Logger.comment("开始验证orderSettlement，验证orderId："
				+ expectOrderSettlement.getOrder_id());

		DataUtil.verifyEquals(expectOrderSettlement.getOrder_id(),
				actualOrderSettlement.getOrder_id(), "验证Order_id");
		DataUtil.verifyEquals(expectOrderSettlement.getPayment_id(),
				actualOrderSettlement.getPayment_id(), "验证Payment_id");
		DataUtil.verifyEquals(
				new BigDecimal(expectOrderSettlement.getExchange_rate())
						.stripTrailingZeros().toPlainString(), new BigDecimal(
						actualOrderSettlement.getExchange_rate())
						.stripTrailingZeros().toPlainString(),
				"验证Exchange_rate");
		DataUtil.verifyDecimalEquals(expectOrderSettlement.getTotal_amount(),
				actualOrderSettlement.getTotal_amount(), "验证Total_amount");
		DataUtil.verifyDecimalEquals(
				Utils.emptyIfNull(expectOrderSettlement.getCurrency())
						.equalsIgnoreCase("JPY") == true ? expectOrderSettlement
						.getForeign_amount().setScale(0, BigDecimal.ROUND_DOWN)
						: expectOrderSettlement.getForeign_amount().setScale(2,
								BigDecimal.ROUND_DOWN), actualOrderSettlement
						.getForeign_amount(), "验证Foreign_amount");
		DataUtil.verifyEquals(expectOrderSettlement.getCurrency(),
				actualOrderSettlement.getCurrency(), "验证Currency");
		DataUtil.verifyEquals(expectOrderSettlement.getHp_settled(),
				actualOrderSettlement.getHp_settled(), "验证Hp_settled");
		DataUtil.verifyEquals(expectOrderSettlement.getSettlement_status(),
				actualOrderSettlement.getSettlement_status(),
				"验证Settlement_status");
		DataUtil.verifyEquals(
				expectOrderSettlement.isIs_foreign_exchange_bid(),
				actualOrderSettlement.isIs_foreign_exchange_bid(),
				"验证Is_foreign_exchange_bid");

		if (settled) {
			Logger.verifyEquals(true,
					Utils.calcTimeSpanMin(actualOrderSettlement
							.getSettlement_time()) < 2,
					"验证数据库Settlement_time跟系统当前时间差为2分钟内");
		} else
			Logger.verifyEquals(null,
					actualOrderSettlement.getSettlement_time(),
					"验证数据库Settlement_time跟系统当前时间差为2分钟内");
		
		if (expectOrderSettlement.getPay_channel_type() == 5 || expectOrderSettlement.getPay_channel_type() == 3)
			DataUtil.verifyEquals(null,
					actualOrderSettlement.getSettlement_result(),
					"验证Settlement_result");
		else
			DataUtil.verifyEquals(
					expectOrderSettlement.getSettlement_status() == 1 ? 0
							: expectOrderSettlement.getSettlement_result(),
					actualOrderSettlement.getSettlement_result(),
					"验证Settlement_result");
		
		DataUtil.verifyEquals(expectOrderSettlement.getIs_rollback(),
				actualOrderSettlement.getIs_rollback(), "验证Is_rollback");
		DataUtil.verifyEquals(expectOrderSettlement.getRollback_result(),
				actualOrderSettlement.getRollback_result(), "验证Rollback_result");
		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(actualOrderSettlement.getAdd_time()) < 2,
				"验证数据库Add_time跟系统当前时间差为2分钟内");
		Logger.verifyEquals(
				true,
				Utils.calcTimeSpanMin(actualOrderSettlement.getUpdate_time()) < 2,
				"验证数据库Update_time跟系统当前时间差为2分钟内");
		DataUtil.verifyDecimalEquals(
				expectOrderSettlement.getBatch_exchange_amount(),
				actualOrderSettlement.getBatch_exchange_amount(),
				"验证Batch_exchange_amount");
		DataUtil.verifyDecimalEquals(
				expectOrderSettlement.getBatch_origin_foreign_amount(),
				actualOrderSettlement.getBatch_origin_foreign_amount(),
				"验证Batch_origin_foreign_amount");
		DataUtil.verifyEquals(expectOrderSettlement.getIs_personal_applet(),
				actualOrderSettlement.getIs_personal_applet() == null ? false
						: actualOrderSettlement.getIs_personal_applet(),
				"验证Is_personal_applet");

		DataUtil.verifyEquals(expectOrderSettlement.getBid_currency(),
				actualOrderSettlement.getBid_currency(), "验证Bid_currency");

		DataUtil.verifyEquals(expectOrderSettlement.getPay_channel_type(),
				actualOrderSettlement.getPay_channel_type(),
				"验证Pay_channel_type");
		DataUtil.verifyEquals(
				expectOrderSettlement.getCustomer_payment_order_no(),
				actualOrderSettlement.getCustomer_payment_order_no(),
				"验证Customer_payment_order_no()");
		DataUtil.verifyDecimalEquals(
				expectOrderSettlement.getCustomer_payment_order_amount(),
				actualOrderSettlement.getCustomer_payment_order_amount(),
				"验证Customer_payment_order_amount");

		DataUtil.verifyEquals(expectOrderSettlement.getPay_mode_type(),
				actualOrderSettlement.getPay_mode_type(), "验证Pay_mode_type");

		DataUtil.verifyEquals(expectOrderSettlement
				.getMore_shop_custom_settlement_currency_type(),
				actualOrderSettlement
						.getMore_shop_custom_settlement_currency_type(),
				"验证More_shop_custom_settlement_currency_type");
		Logger.verifyEquals(expectOrderSettlement.getOrder_type(),
				actualOrderSettlement.getOrder_type(), "验证Order_type");
	}

	public static void verifyOrderSettlementDetailTable(
			OrderSettlementDetail expectDetailPo,
			OrderSettlementDetail actualDetailPo) {

		Logger.comment("开始验证orderSettlementDetail，验证mchId："
				+ expectDetailPo.getUser_id() + "， settlementType："
				+ expectDetailPo.getSettlement_type());
		DataUtil.verifyEquals(expectDetailPo.getOrder_id(),
				actualDetailPo.getOrder_id(), "验证Order_id");
		DataUtil.verifyEquals(expectDetailPo.getUser_id(),
				actualDetailPo.getUser_id(), "验证User_id");
		DataUtil.verifyDecimalEquals(expectDetailPo.getTotal_rmb_amount(),
				actualDetailPo.getTotal_rmb_amount(), "验证Total_rmb_amount");
		DataUtil.verifyDecimalEquals(expectDetailPo.getTotal_foreign_amount(),
				actualDetailPo.getTotal_foreign_amount(),
				"验证Total_foreign_amount");
		DataUtil.verifyEquals(expectDetailPo.getForeign_currency(),
				actualDetailPo.getForeign_currency(), "验证Foreign_currency");
		DataUtil.verifyEquals(new BigDecimal(expectDetailPo.getExchange_rate())
				.stripTrailingZeros().toPlainString(), new BigDecimal(
				actualDetailPo.getExchange_rate()).stripTrailingZeros()
				.toPlainString(), "验证Exchange_rate");
		DataUtil.verifyDecimalEquals(expectDetailPo.getPlatform_fee_rate().setScale(2, BigDecimal.ROUND_HALF_UP),
				actualDetailPo.getPlatform_fee_rate(), "验证Platform_fee_rate");
		DataUtil.verifyDecimalEquals(expectDetailPo.getPlatform_fee(),
				actualDetailPo.getPlatform_fee(), "验证Platform_fee");
		DataUtil.verifyEquals(expectDetailPo.getSettlement_currency(),
				actualDetailPo.getSettlement_currency(),
				"验证Settlement_currency");
		DataUtil.verifyDecimalEquals(expectDetailPo.getSettlement_amount(),
				actualDetailPo.getSettlement_amount(), "验证Settlement_amount");
		DataUtil.verifyEquals(expectDetailPo.getSettlement_status(),
				actualDetailPo.getSettlement_status(), "验证Settlement_status");
		DataUtil.verifyEquals(expectDetailPo.getIs_rollback(),
				actualDetailPo.getIs_rollback(), "验证Is_rollback");
		DataUtil.verifyEquals(expectDetailPo.getIs_proxy_product(),
				actualDetailPo.getIs_proxy_product(), "验证Is_proxy_product");
		DataUtil.verifyEquals(expectDetailPo.getGoods_owner_id(),
				actualDetailPo.getGoods_owner_id(), "验证Goods_owner_id");
		DataUtil.verifyEquals(expectDetailPo.getParent_proxy_id(),
				actualDetailPo.getParent_proxy_id(), "验证Parent_proxy_id");
		DataUtil.verifyEquals(expectDetailPo.getUser_role(),
				actualDetailPo.getUser_role(), "验证User_role");
		DataUtil.verifyEquals(expectDetailPo.getRemark(),
				actualDetailPo.getRemark(), "验证Remark");
		DataUtil.verifyEquals(expectDetailPo.getIs_personal_applet(),
				actualDetailPo.getIs_personal_applet() == null ? false
						: actualDetailPo.getIs_personal_applet(),
				"验证Is_personal_applet");
		
		DataUtil.verifyEquals(expectDetailPo.getIs_dependent_merchant(),
				actualDetailPo.getIs_dependent_merchant(),
				"验证Is_dependent_merchant");
		
		DataUtil.verifyEquals(expectDetailPo.getNeed_exchange(),
				actualDetailPo.getNeed_exchange(),
				"验证Need_exchange");
		DataUtil.verifyEquals(expectDetailPo.getUser_country(),
				actualDetailPo.getUser_country(),
				"验证User_country");
		DataUtil.verifyEquals(expectDetailPo.getUser_currency(),
				actualDetailPo.getUser_currency(),
				"验证User_currency");
		DataUtil.verifyDecimalEquals(
				new BigDecimal(expectDetailPo
						.getForeign_and_bid_exchange_rate())
						.stripTrailingZeros(),
				new BigDecimal(actualDetailPo
						.getForeign_and_bid_exchange_rate())
						.stripTrailingZeros(),
				"验证Foreign_and_bid_exchange_rate");
		
		DataUtil.verifyEquals(expectDetailPo.getCustomer_payment_order_no(),
				actualDetailPo.getCustomer_payment_order_no(),
				"验证Customer_payment_order_no");
		DataUtil.verifyEquals(expectDetailPo.getCustomer_payment_order_amount(),
				actualDetailPo.getCustomer_payment_order_amount(),
				"验证Customer_payment_order_amount");
		DataUtil.verifyEquals(expectDetailPo.getPay_channel_type(),
				actualDetailPo.getPay_channel_type(),
				"验证Pay_channel_type");
		
		DataUtil.verifyEquals(Utils.falseIfNull(expectDetailPo.getIs_proxy_share_fee_merchant()),
				actualDetailPo.getIs_proxy_share_fee_merchant(),
				"验证Is_proxy_share_fee_merchant");
		DataUtil.verifyDecimalEquals(
				expectDetailPo.getForeign_currency().equalsIgnoreCase("JPY") ? expectDetailPo
						.getOrder_foreign_amount().setScale(0,
								BigDecimal.ROUND_DOWN) : expectDetailPo
						.getOrder_foreign_amount().setScale(2,
								BigDecimal.ROUND_DOWN), actualDetailPo
						.getOrder_foreign_amount(), "验证Order_foreign_amount");
		
		DataUtil.verifyEquals(expectDetailPo.getPay_mode_type(),
				actualDetailPo.getPay_mode_type(),
				"验证Pay_mode_type");
		
		DataUtil.verifyEquals(expectDetailPo.getPayment_id(),
				actualDetailPo.getPayment_id(),
				"验证Payment_id");
		
		DataUtil.verifyEquals(expectDetailPo.getMore_shop_custom_settlement_currency_type(),
				actualDetailPo.getMore_shop_custom_settlement_currency_type(),
				"验证More_shop_custom_settlement_currency_type");
		
		DataUtil.verifyDecimalEquals(
				expectDetailPo.getBid_vs_settlement_exchange_rate(),
				actualDetailPo.getBid_vs_settlement_exchange_rate(),
				"验证Bid_vs_settlement_exchange_rate");
		
		DataUtil.verifyEquals(expectDetailPo.getSettlement_type(),
				actualDetailPo.getSettlement_type(),
				"验证Settlement_type");
		DataUtil.verifyEquals(expectDetailPo.getBiz_id(),
				actualDetailPo.getBiz_id(),
				"验证Biz_id");
		
		DataUtil.verifyEquals(expectDetailPo.getFund_not_collect_merchant_id(),
				actualDetailPo.getFund_not_collect_merchant_id(),
				"验证Fund_not_collect_merchant_id");
		
		DataUtil.verifyEquals(expectDetailPo.getGoods_owner_settlement_config_id(),
				actualDetailPo.getGoods_owner_settlement_config_id(),
				"验证Goods_owner_settlement_config_id");
		
		DataUtil.verifyEquals(expectDetailPo.getGoods_owner_settlement_period_type(),
				actualDetailPo.getGoods_owner_settlement_period_type(),
				"验证Goods_owner_settlement_period_type");
		
		DataUtil.verifyEquals(expectDetailPo.getGoods_owner_settlement_expected_time(),
				actualDetailPo.getGoods_owner_settlement_expected_time(),
				"验证Goods_owner_settlement_expected_time");
		
		DataUtil.verifyEquals(expectDetailPo.getDependent_merchant_id(),
				actualDetailPo.getDependent_merchant_id(),
				"验证Dependent_merchant_id");
		
		DataUtil.verifyEquals(expectDetailPo.getSettlement_way(),
				actualDetailPo.getSettlement_way(),
				"验证Settlement_way");
		
		Logger.verifyEquals(expectDetailPo.getIs_platform_supplier(),
				actualDetailPo.getIs_platform_supplier(),
				"验证Is_platform_supplier");
		
		// 校验资金流水
		AccountWapper accountWapper = new AccountWapper();

		if (actualDetailPo.getSettlement_status() == 1
				&& null != actualDetailPo.getSettlement_time()
				&& actualDetailPo.getSettlement_amount().compareTo(
						BigDecimal.ZERO) != 0
				&& actualDetailPo.getSettlement_way() == 0
				&& actualDetailPo.getPay_channel_type() != 3
				&& actualDetailPo.getPay_channel_type() != 5
				&& expectDetailPo.getPay_channel_type() != 61) {
			
			int accountType = ShenqiSettlementBuildVerifyService.getAccountType(
					expectDetailPo.getPay_channel_type(), expectDetailPo
							.getSettlement_currency(), Utils
							.zeroIfNull(expectDetailPo
									.getFund_not_collect_merchant_id()) > 0);
			
			HashMap<String, Object> accountEntryMap = new HashMap<>();

			accountEntryMap.put("userId", expectDetailPo.getUser_id());
			accountEntryMap.put("bizcode", "360004");
			accountEntryMap.put("originalNo", expectDetailPo.getOrder_id());
			accountEntryMap.put("accountType", accountType);
			
			if (expectDetailPo.getSettlement_type() == 1) {
				
				accountEntryMap.put("bizcode", "360006");
				accountEntryMap.put("bizno", expectDetailPo.getBiz_id());
			} else if (expectDetailPo.getSettlement_type() == 0) {
				
				accountEntryMap.put("bizcode", "360004");
				accountEntryMap.put("bizno",
						String.valueOf(expectDetailPo.getOrder_id()));
			} else if (expectDetailPo.getSettlement_type() == 4) {
				
				accountEntryMap.put("bizcode", "360049");
				accountEntryMap.put("bizno",
						String.valueOf(expectDetailPo.getBiz_id()));
			} else if (expectDetailPo.getSettlement_type() == 5) {
				
				accountEntryMap.put("bizcode", "370037");
				accountEntryMap.put("bizno",
						String.valueOf(expectDetailPo.getBiz_id()));
			} else if (expectDetailPo.getSettlement_type() == 6) {
				
				accountEntryMap.put("bizcode", "370037");
				accountEntryMap.put("bizno",
						String.valueOf(expectDetailPo.getBiz_id()));
			} else if (expectDetailPo.getSettlement_type() == 9) {
				
				accountEntryMap.put("bizcode", "360062");
				accountEntryMap.put("bizno",
						String.valueOf(expectDetailPo.getBiz_id()));
			} else if (expectDetailPo.getSettlement_type() == 10) {
				
				accountEntryMap.put("bizcode", "360063");
				accountEntryMap.put("bizno",
						String.valueOf(expectDetailPo.getBiz_id()));
			} else if (expectDetailPo.getSettlement_type() == 11) {
				
				accountEntryMap.put("bizcode", "360065");
				accountEntryMap.put("bizno",
						String.valueOf(expectDetailPo.getBiz_id()));
			} else if (expectDetailPo.getSettlement_type() == 12) {
				
				accountEntryMap.put("bizcode", "360039");
				accountEntryMap.put("bizno",
						String.valueOf(expectDetailPo.getOrder_id()));
			} else {

				accountEntryMap.put("bizcode", "360049");
				accountEntryMap.put("bizno",
						String.valueOf(expectDetailPo.getBiz_id()));
			}
			
			DataUtil.verifyDecimalEquals(
					actualDetailPo.getSettlement_amount().setScale(2, BigDecimal.ROUND_DOWN),
					accountWapper.selectAccountEntryByMap(accountEntryMap)
							.get(0).getAmount(), "验证accountEntry日志记录");
			
		} else if (expectDetailPo.getSettlement_status() == 2
				&& null != expectDetailPo.getSettlement_time()
				&& expectDetailPo.getSettlement_amount().compareTo(
						BigDecimal.ZERO) == 1) {
			
			int accountType = ShenqiSettlementBuildVerifyService.getAccountType(
					expectDetailPo.getPay_channel_type(), expectDetailPo
							.getSettlement_currency(), Utils
							.zeroIfNull(expectDetailPo
									.getFund_not_collect_merchant_id()) > 0);

			HashMap<String, Object> accountEntryMap = new HashMap<>();

			accountEntryMap.put("userId", expectDetailPo.getUser_id());
			accountEntryMap.put("bizcode", "360004");
			accountEntryMap.put("originalNo", expectDetailPo.getOrder_id());
			accountEntryMap.put("accountType", accountType);

			DataUtil.verifyDecimalEquals(actualDetailPo.getSettlement_amount(), accountWapper
					.selectAccountEntryByMap(accountEntryMap).get(0)
					.getAmount(), "验证accountEntry日志记录");

			accountEntryMap.put("bizcode", "360005");

			DataUtil.verifyDecimalEquals(
					actualDetailPo.getSettlement_amount().negate(),
					accountWapper.selectAccountEntryByMap(accountEntryMap)
							.get(0).getAmount(), "验证accountEntry日志记录");
		}
	}

	public static void verifyHpSettlemenNotify(OnHpSettledReq hpSettledReq,
			BigDecimal ymtSharingAmount) {

		Logger.comment("验证hp_settlement_notify， payment_id："
				+ hpSettledReq.getTradingId());

		HpSettlementNotifyWrapper hpSettlementNotifyWrapper = new HpSettlementNotifyWrapper();

		HpSettlementNotify actualNotify = hpSettlementNotifyWrapper
				.selectByPaymentId(hpSettledReq.getTradingId());

		DataUtil.verifyDecimalEquals(
				Utils.zeroIfNull(hpSettledReq.getSettleFee()).setScale(2,
						BigDecimal.ROUND_HALF_UP),
				actualNotify.getPayment_fee_amount(), "验证payment_fee_amount");
	}
	
	public static void verifyPlatformProductDetailTable(
			PlatformProductDetail expectPlatformProductDetail,
			PlatformProductDetail actualPlatformProductDetail) {

		Logger.comment("开始验证PlatformProductDetailTable，验证orderId："
				+ expectPlatformProductDetail.getOrder_id() + "， catalogId："
				+ expectPlatformProductDetail.getCatalog_id());
		
		Logger.verifyEquals(expectPlatformProductDetail.getOrder_id(),
				actualPlatformProductDetail.getOrder_id(), "验证Order_id");
		Logger.verifyEquals(expectPlatformProductDetail.getPlatform_id(),
				actualPlatformProductDetail.getPlatform_id(), "验证Platform_id");
		DataUtil.verifyDecimalEquals(expectPlatformProductDetail.getPlatform_rate(),
				actualPlatformProductDetail.getPlatform_rate(), "验证Platform_rate");
		Logger.verifyEquals(expectPlatformProductDetail.getProduct_id(),
				actualPlatformProductDetail.getProduct_id(), "验证Product_id");
		Logger.verifyEquals(expectPlatformProductDetail.getCatalog_id(),
				actualPlatformProductDetail.getCatalog_id(), "验证Catalog_id");
		DataUtil.verifyDecimalEquals(expectPlatformProductDetail.getProduct_price(),
				actualPlatformProductDetail.getProduct_price(), "验证Product_price");
		Logger.verifyEquals(expectPlatformProductDetail.getProduct_num(),
				actualPlatformProductDetail.getProduct_num(), "验证Product_num");
		Logger.verifyEquals(expectPlatformProductDetail.getBid_currency(),
				actualPlatformProductDetail.getBid_currency(), "验证Bid_currency");
		Logger.verifyEquals(expectPlatformProductDetail.getSettlement_currency(),
				actualPlatformProductDetail.getSettlement_currency(), "验证Settlement_currency");
		Logger.verifyEquals(expectPlatformProductDetail.getMch_fee_config_id(),
				actualPlatformProductDetail.getMch_fee_config_id(), "验证Mch_fee_config_id");
	}
}
